An immutable variable in a program is a variable which does not change after it is initialized or initially assigned to. Immutable variables allow for optimizations in which a variable's value can be copied and cached rather than re-examined; and language semantics in which the sequencing of variable references is irrelevant.
In comparison compiling code which includes mutable variables which are referenced by higher order functions may result in runtime errors. A mutable variable may change in value before or after it is utilized in a higher order function which references the variable. This can result in a race condition at runtime. For example, thread-safety issues may arise when multiple threads are trying to modify an object's state concurrently (write-write conflicts) or when one thread is trying to access an object's state while another thread is modifying it (read-write conflicts.) Thus, it is useful for a language to prohibit certain accesses to mutable variables in order to prevent runtime errors. In some systems the compiler identifies possibly mutable local variables and throws an error for each possibly mutable local variable such that the programmer has to expressly declare that the variable is immutable using, for example, a keyword.
The Java (Java™ is a registered trademark of Oracle America, Inc.) programming language is a popular programming language. In the Java programming language immutable variables can be expressly declared to be ‘final’ variables. In previous versions of Java, only local variables that were expressly declared ‘final’ by a programmer could be referred to an anonymous class declaration. The programmer was thus required to expressly indicate that the local variable was immutable by declaring it as ‘final’. This requirement increased syntax bulk and work for the programmer.